package com.sfx.concurrent.demo.concurrent.AtomicTest.demo2;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-18
 * Time: 19:06
 */
public interface DecimalAccount {

    //获取金额
    BigDecimal getBalance();

    //取款操作
    void withDraw(BigDecimal money);

    //demo方法,创建1000个线程,每一个线程取款10元,总共有10000元
    static void demo(DecimalAccount account) {
        List<Thread> list = new ArrayList<>();
        long start = System.nanoTime();//记录开始时间
        for(int i =0;i<1000;++i) {
            list.add(new Thread(()->{
                account.withDraw(BigDecimal.TEN);
            }));
        }
        //对list中每一个线程开启
        list.forEach(Thread::start);

        //等待list中所有线程执行完成
        list.forEach(t->{
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.nanoTime();
        System.out.println("剩余金额 : " + account.getBalance()
                + " 所需时间 : " + (end-start)/1000_000);
    }
}
